package com.ruoyi.factorydata.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.factorydata.domain.CustomerTypeData;
import com.ruoyi.factorydata.service.ICustomerTypeDataService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 客户类型Controller
 *
 * @author ruoyi
 * @date 2025-07-03
 */
@RestController
@RequestMapping("/customertypedata/customertypedata")
public class CustomerTypeDataController extends BaseController {
    @Autowired
    private ICustomerTypeDataService customerTypeDataService;

    /**
     * 查询客户类型列表
     */
    @PreAuthorize("@ss.hasPermi('customertypedata:customertypedata:list')")
    @GetMapping("/list")
    public TableDataInfo list(CustomerTypeData customerTypeData) {
        startPage();
        List<CustomerTypeData> list = customerTypeDataService.selectCustomerTypeDataList(customerTypeData);
        return getDataTable(list);
    }

    /**
     * 导出客户类型列表
     */
    @PreAuthorize("@ss.hasPermi('customertypedata:customertypedata:export')")
    @Log(title = "客户类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CustomerTypeData customerTypeData) {
        List<CustomerTypeData> list = customerTypeDataService.selectCustomerTypeDataList(customerTypeData);
        ExcelUtil<CustomerTypeData> util = new ExcelUtil<CustomerTypeData>(CustomerTypeData.class);
        util.exportExcel(response, list, "客户类型数据");
    }

    /**
     * 获取客户类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('customertypedata:customertypedata:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(customerTypeDataService.selectCustomerTypeDataById(id));
    }

    /**
     * 新增客户类型
     */
    @PreAuthorize("@ss.hasPermi('customertypedata:customertypedata:add')")
    @Log(title = "客户类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CustomerTypeData customerTypeData) {
        boolean exists = customerTypeDataService.lambdaQuery().eq(CustomerTypeData::getTypeName, customerTypeData.getTypeName()).count() > 0;
        if (exists) {
            return AjaxResult.warn("客户类型已存在");
        }
        customerTypeData.setCreateBy(getUserId().toString());
        return toAjax(customerTypeDataService.insertCustomerTypeData(customerTypeData));
    }

    /**
     * 修改客户类型
     */
    @PreAuthorize("@ss.hasPermi('customertypedata:customertypedata:edit')")
    @Log(title = "客户类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CustomerTypeData customerTypeData) {
        boolean exists = customerTypeDataService.lambdaQuery()
                .ne(CustomerTypeData::getId, customerTypeData.getId())
                .eq(CustomerTypeData::getTypeName, customerTypeData.getTypeName())
                .count() > 0;
        if (exists) {
            return error("客户类型已存在");
        }
        return toAjax(customerTypeDataService.updateCustomerTypeData(customerTypeData));
    }

    /**
     * 删除客户类型
     */
    @PreAuthorize("@ss.hasPermi('customertypedata:customertypedata:remove')")
    @Log(title = "客户类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(customerTypeDataService.deleteCustomerTypeDataByIds(ids));
    }
}
